package com.vito.dcs.acquisition.blender;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketAddress;
import java.util.Arrays;
import java.util.Date;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.vito.base.factory.IDFactory;
import com.vito.dcs.water.waterData.dao.DataWaterBlenderMapper;
import com.vito.dcs.water.waterData.model.DataWaterBlenderModel;
import com.vito.dcs.water.waterDataLog.dao.DataWaterBlenderLogMapper;
import com.vito.dcs.water.waterDataLog.model.DataWaterBlenderLogModel;

/***
 * 监听水稳搅拌上报数据
 * @author hug19
 *
 */
@Component
public class AcquisitionWaterListener implements InitializingBean {

	@Autowired
	private DataWaterBlenderLogMapper dataWaterBlenderLogMapper;
	
	@Autowired
	private DataWaterBlenderMapper dataWaterBlenderMapper;
	
	@Override
	public void afterPropertiesSet() throws Exception {
		
		Thread thread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				int port = 6067;
				DatagramSocket getSocket = null;
				try {
					InetAddress ip = InetAddress.getLocalHost();
					//System.out.println("ip地址："+ip);
					getSocket = new DatagramSocket(port, ip); 
					byte[] buf = null;
					byte[] data = null;
					byte[] feedBackData = null;
					DatagramPacket getPacket = null;
					DatagramPacket sendPacket = null;
					SocketAddress sendAddress = null;
					String hexString = null;
					String feedBackStr = null;
					String logId = null;
					String dataId = null;
					DataWaterBlenderLogModel dataWaterBlenderLog = null;
					while(true) {
						
						buf = new byte[1024];
						getPacket = new DatagramPacket(buf, buf.length);
						getSocket.receive(getPacket);
						//data = getPacket.getData();
						data = Arrays.copyOfRange(getPacket.getData(),getPacket.getOffset(),
								getPacket.getOffset() + getPacket.getLength());
						hexString = UdpUtil.bytesToHexString(data);
						System.out.println(hexString);
						DataWaterBlenderModel waterData = UdpUtil.analysisWater(hexString);
						if(waterData!=null) {
							logId = IDFactory.getIDFactory().getIdInc("tbl_dcs_data_water_blender_log")+"";
							dataWaterBlenderLog = new DataWaterBlenderLogModel();
							dataWaterBlenderLog.setId(logId);
							dataWaterBlenderLog.setHexstring(hexString);
							dataWaterBlenderLog.setIsHandler("1");
							dataWaterBlenderLog.setCreateDate(new Date());
							dataWaterBlenderLogMapper.save(dataWaterBlenderLog);
							
				            sendAddress = getPacket.getSocketAddress();       
				            feedBackStr = hexString.substring(0,30);
				            feedBackData = UdpUtil.hexStringToBytes(feedBackStr);
				            sendPacket = new DatagramPacket(feedBackData,  
				            		feedBackData.length, sendAddress);  	  
				            getSocket.send(sendPacket);
				            dataId = IDFactory.getIDFactory().getIdInc("tbl_dcs_data_water_blender")+"";
				            waterData.setId(dataId);
				            waterData.setLogId(logId);
				            waterData.setIsHandler("0");
				            dataWaterBlenderMapper.save(waterData);
				            
						}
						 
			            
						
					}
				}catch(Exception ex){
					ex.printStackTrace();
				}finally {
					if(getSocket!=null) {
						getSocket.close();
					}
				}
				
			}
		});
		thread.start();

	}

}
